package com.dbq.fast.business.welcome;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;

import com.dbq.fast.App;
import com.dbq.fast.R;
import com.dbq.fast.analysis.EventConstant;
import com.dbq.fast.analysis.YibaAnalysis;
import com.dbq.fast.business.h5.H5ContainerActivity;
import com.dbq.fast.business.login.NewLoginActivity;
import com.dbq.fast.business.main.MainActivity;
import com.dbq.fast.common.base.BaseActivity;
import com.dbq.fast.util.SpUtil;
import com.dbq.fast.util.SystemUtil;

import static com.dbq.fast.analysis.EventConstant.PARAM_NET_MOBILE;
import static com.dbq.fast.analysis.EventConstant.PARAM_NET_NONE;
import static com.dbq.fast.analysis.EventConstant.PARAM_NET_WIFI;
import static com.dbq.fast.business.h5.H5ContainerActivity.WHICH_IS_PRIVACY;

public class WelcomeActivity extends BaseActivity{

    private final static int START_VPN_LOGIN_REQUEST_CODE = 100;

    private static final String HAS_LOGIN_SUCCESS = "hasLoginSuceess";

    Button goMainBtn;
    CheckBox checkBox;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        boolean flag = SpUtil.getBoolean(HAS_LOGIN_SUCCESS, false);
        if(flag){
            goMainPage();
        }else {
            YibaAnalysis.getInstance().event(App.getContext(), EventConstant.EVENT_OPEN_WELCOME);
        }
        getWindow().setBackgroundDrawableResource(R.drawable.bg_welcome);
        setContentView(R.layout.activity_welcome);

        goMainBtn = (Button) findViewById(R.id.go_main);
        checkBox = (CheckBox) findViewById(R.id.checkbox);
        checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                goMainBtn.setEnabled(true);
            }else {
                goMainBtn.setEnabled(false);
            }
        });

        goMainBtn.setOnClickListener(v -> {
            if (checkBox.isChecked()) {
                login();
                Bundle bundle = new Bundle();
                if (!SystemUtil.isNetworkAvailable(App.getContext())) {
                    bundle.putString("login_net_status", PARAM_NET_NONE);
                }else {
                    if(SystemUtil.isWifiAvailable(App.getContext())){
                        bundle.putString("login_net_status", PARAM_NET_WIFI);
                    }else {
                        bundle.putString("login_net_status", PARAM_NET_MOBILE);
                    }
                }
                YibaAnalysis.getInstance().event(App.getContext(), EventConstant.EVENT_CLICK_TO_LOGIN, bundle);
            }
        });
        findViewById(R.id.user_agreement).setOnClickListener(v -> {
            goPrivacyPage();
        });

    }

    private void goPrivacyPage() {
        Intent intent = new Intent(WelcomeActivity.this, H5ContainerActivity.class);
        intent.putExtra("which", WHICH_IS_PRIVACY);
        startActivity(intent);
    }

    private void goMainPage(){
        SpUtil.setBoolean(HAS_LOGIN_SUCCESS, true);
        startActivity(new Intent(WelcomeActivity.this, MainActivity.class));
        finish();
    }

    private void login(){
        Intent intent = new Intent(this, NewLoginActivity.class);
        intent.putExtra("goLogin", true);
        startActivityForResult(intent, START_VPN_LOGIN_REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == START_VPN_LOGIN_REQUEST_CODE){
            if(resultCode == RESULT_OK){
                goMainPage();
            }
        }
    }
}
